angular.module('myApp.science_sensor', ['ngRoute'])

    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/testing/science_sensor', {
            templateUrl: 'pages/testing/science_sensor/science_sensor.html',
            controller: 'ScienceSensorCtrl'
        });
    }])

    .controller('ScienceSensorCtrl', function ($scope, $location, $rootScope) {
        $rootScope.show_header = false
        $rootScope.show_footer = false
        $rootScope.title = '科学传感器'
        $rootScope.items = []
        console.log($location.path(), ' entered')
        $scope.controller = null

        graph = new Graph(document.getElementById('myChart'));
        loadAllSettings();
        createChart();
        graph.adaChart.options.title = {
            display: true,
            padding: 5,
            text: ''
        }

        let content = document.querySelector('#content')
        content.classList.add('fullscreen')

        $scope.selected = ''

        let bufferSize = [25, 50, 100, 250, 500, 1000]
        let bufferMode = 1

        function changePlotType(type) {
            if (graph.plotType != type) {
                graph.setPlotType(type);
                reset();
                createChart();
            }
        }

        function switchDataSource(index) {
            // if($scope.selected == sensorState[index].sensor_name){
            //     return
            // }
            if ($scope.controller) {
                $scope.controller.abort()
            }
            setTimeout(() => {
                $scope.controller = new AbortController()
                reset()
                changePlotType('xt');
                graph.setBufferSize(bufferSize[bufferMode])
                graph.adaChart.options.title.text = sensorState[index].sensor_name
                $scope.selected = sensorState[index].sensor_name
                readDataLoop(sensorState[index].port, $scope.controller, () => {
                    if ($location.path() != '/testing/science_sensor') {
                        $scope.controller.abort()
                        $scope.controller = null
                    }
                })
            }, 30)

        }

        $rootScope.localMenus[$location.path()] = [
            // {
            //     text: 'Vout2',
            //     callback: (index) => {
            //         switchDataSource('Vout2')
            //     }
            // },
        ]

        const localHandler = (e) => {
            var i = $scope.activeId
            switch (e.code) {
                case "ArrowLeft":
                    if (bufferMode > 0) {
                        bufferMode--
                    }

                    break;
                case "ArrowRight":
                    if (bufferMode < bufferSize.length - 1) {
                        bufferMode++
                    }
                    break;
                default:
                    return false
            }
            graph.setBufferSize(bufferSize[bufferMode])
            swal.fire({
                title: `缓存点数:${parseInt(bufferSize[bufferMode])}`,
                timer: 1500,
            })
            // if ($scope.selected > 0) {
            //     switchDataSource(`传感器S${$scope.selected}`)
            // } else {
            //     switchDataSource($scope.selected)
            // }
            console.log($scope.selected)
            return true
        }

        $rootScope.localHandler[$location.path()] = localHandler

        $rootScope.updatePageInfo()

        reset()


        connectSensors((sensor_name, index) => {
            if ($location.path() == '/testing/science_sensor') {
                $rootScope.localMenus[$location.path()].push({
                    text: sensor_name,
                    callback: () => {
                        switchDataSource(index)
                    }
                })
                $rootScope.updatePageInfo()
            }
            console.log(sensor_name, index, $location.path(), $rootScope.localMenus)
        })

        // connect()

    })
